package cn.zcib.controller;

import cn.zcib.domain.Student;
import cn.zcib.service.StudentService;

import java.util.Scanner;

/**
 * @Author Lucky赵
 * @Date 2021年08月11日 9:12
 */
public class StudentController {
    private static final Scanner sc = new Scanner(System.in);
    private static final StudentService stuService = new StudentService();
    private static final Student student = new Student();

    //启动学生管理，并展示学生管理菜单
    public void start() {
        studentLoop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    addStu();
                    break;
                case 2:
                    delStuById();
                    break;
                case 3:
                    updateStu();
                    break;
                case 4:
                    findAllStu();
                    break;
                case 5:
                    System.out.println("感谢您使用学生管理");
                    break studentLoop;
                default:
                    System.out.println("请输入正确的信息");
                    break;
            }
        }
    }

    private void updateStu() {
        String id = inputId();
        Student stu = inputInfo(id);
        stuService.updateStu(id, stu);
        System.out.println("修改成功");
    }

    private void delStuById() {
        String id = inputId();
        //调用StudentService中的delStuById()方法根据id删除学生信息
        stuService.delStuById(id);
        //提示删除成功
        System.out.println("删除成功！");
    }

    private void findAllStu() {
        //调用StudentService中的findAllStu()方法获得学生对象数组
        Student[] students = stuService.findAllStu();
        //判断数组的内存信息
        if (students == null) {
            System.out.println("查无信息，请重试！");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (Student stu : students) {
            if (stu != null)
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
        }
    }

    private void addStu() {
        String id;
        while (true) {
            //键盘接受学生信息
            System.out.print("请输入学生id：");
            id = sc.next();
            boolean flag = stuService.isExists(id);
            if (flag)
                System.out.println("学号已被占用，请重新输入。");
            else
                break;
        }
        Student stu = inputInfo(id);
        //调用StudentService中的addStu()方法传递学生对象
        boolean result = stuService.addStu(stu);
        //根据Boolean类型结果，在控制台打印成功/失败
        if (result)
            System.out.println("添加成功！");
        else
            System.out.println("添加失败！");
    }

    /**
     * 键盘录入学生id
     *
     * @return 返回学生id
     */
    private String inputId() {
        String id;
        while (true) {
            System.out.print("请输入学生id：");
            id = sc.next();
            boolean exists = stuService.isExists(id);
            //判断id是否在容器中存在，不存在就重新录入
            if (!exists)
                System.out.println("请您输入正确的id信息！");
            else
                break;
        }
        return id;
    }

    /**
     * 键盘录入学生信息
     *
     * @param id 学生id
     * @return 学生对象
     */
    private Student inputInfo(String id) {
        System.out.print("请输入学生姓名：");
        String name = sc.next();
        System.out.print("请输入学生年龄：");
        int age = sc.nextInt();
        System.out.print("请输入学生生日：");
        String birthday = sc.next();
        //将学生信息封装为student对象
        student.setId(id);
        student.setName(name);
        student.setAge(age);
        student.setBirthday(birthday);
        return student;
    }
}

